home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Dev / gcc263-src.lha / gcc-2.6.3 / scan.c < prev    next >
C/C++ Source or Header  |  1994-04-11  |  5KB  |  280 lines

  1. /* Utility functions for scan-decls and fix-header programs.
  2.    Copyright (C) 1993, 1994 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include "scan.h"
  19. #include "hconfig.h"
  20. #include <ctype.h>
  21.  
  22. int lineno = 1;
  23. int source_lineno = 1;
  24. sstring source_filename;
  25.  
  26. void
  27. make_sstring_space (str, count)
  28.      sstring *str;
  29.      int count;
  30. {
  31.   int cur_pos = str->ptr - str->base;
  32.   int cur_size = str->limit - str->base;
  33.   int new_size = cur_pos + count + 100;
  34.  
  35.   if (new_size <= cur_size)
  36.     return;
  37.   
  38.   if (str->base == NULL)
  39.     str->base = xmalloc (new_size);
  40.   else
  41.     str->base = xrealloc (str->base, new_size);
  42.   str->ptr = str->base + cur_size;
  43.   str->limit = str->base + new_size;
  44. }
  45.  
  46. void
  47. sstring_append (dst, src)
  48.      sstring *dst;
  49.      sstring *src;
  50. {
  51.   register char *d, *s;
  52.   register count = SSTRING_LENGTH(src);
  53.   MAKE_SSTRING_SPACE(dst, count + 1);
  54.   d = dst->ptr;
  55.   s = src->base;
  56.   while (--count >= 0) *d++ = *s++;
  57.   dst->ptr = d;
  58.   *d = 0;  
  59. }
  60.  
  61. memory_full ()
  62. {
  63.   abort();
  64. }
  65.  
  66. char *
  67. xmalloc (size)
  68.      unsigned size;
  69. {
  70.   register char *ptr = (char *) malloc (size);
  71.   if (ptr != 0) return (ptr);
  72.   memory_full ();
  73.   /*NOTREACHED*/
  74.   return 0;
  75. }
  76.  
  77.  
  78. char *
  79. xrealloc (old, size)
  80.      char *old;
  81.      unsigned size;
  82. {
  83.   register char *ptr = (char *) realloc (old, size);
  84.   if (ptr != 0) return (ptr);
  85.   memory_full ();
  86.   /*NOTREACHED*/
  87.   return 0;
  88. }
  89.  
  90. int
  91. scan_ident (fp, s, c)
  92.      register FILE *fp;
  93.      register sstring *s;
  94.      int c;
  95. {
  96.   s->ptr = s->base;
  97.   if (isalpha(c) || c == '_')
  98.     {
  99.       for (;;)
  100.     {
  101.       SSTRING_PUT(s, c);
  102.       c = getc (fp);
  103.       if (c == EOF || !(isalnum(c) || c == '_'))
  104.         break;
  105.     }
  106.     }
  107.   MAKE_SSTRING_SPACE(s, 1);
  108.   *s->ptr = 0;
  109.   return c;
  110. }
  111.  
  112. int
  113. scan_string (fp, s, init)
  114.      register FILE *fp;
  115.      register sstring *s;
  116. {
  117.   int c;
  118.   for (;;)
  119.     {
  120.       c = getc (fp);
  121.       if (c == EOF || c == '\n')
  122.     break;
  123.       if (c == init)
  124.     {
  125.       c = getc (fp);
  126.       break;
  127.     }
  128.       if (c == '\\')
  129.     {
  130.       c = getc (fp);
  131.       if (c == EOF)
  132.         break;
  133.       if (c == '\n')
  134.         continue;
  135.     }
  136.       SSTRING_PUT(s, c);
  137.     }
  138.   MAKE_SSTRING_SPACE(s, 1);
  139.   *s->ptr = 0;
  140.   return c;
  141. }
  142.  
  143. /* Skip horizontal white spaces (spaces, tabs, and C-style comments). */
  144.  
  145. int
  146. skip_spaces (fp, c)
  147.      register FILE *fp;
  148.      int c;
  149. {
  150.   for (;;)
  151.     {
  152.       if (c == ' ' || c == '\t')
  153.     c = getc (fp);
  154.       else if (c == '/')
  155.     {
  156.       c = getc (fp);
  157.       if (c != '*')
  158.         {
  159.           ungetc (c, fp);
  160.           return '/';
  161.         }
  162.       c = getc (fp);
  163.       for (;;)
  164.         {
  165.           if (c == EOF)
  166.         return EOF;
  167.           else if (c != '*')
  168.         {
  169.           if (c == '\n')
  170.             source_lineno++, lineno++;
  171.           c = getc (fp);
  172.         }
  173.           else if ((c = getc (fp)) == '/')
  174.         return getc (fp);
  175.         }
  176.     }
  177.       else
  178.     break;
  179.     }
  180.   return c;
  181. }
  182.  
  183. int
  184. read_upto (fp, str, delim)
  185.      FILE *fp;
  186.      sstring *str;
  187.      int delim;
  188. {
  189.   int ch;
  190.   for (;;)
  191.     {
  192.       ch = getc (fp);
  193.       if (ch == EOF || ch == delim)
  194.     break;
  195.       SSTRING_PUT(str, ch);
  196.     }
  197.   MAKE_SSTRING_SPACE(str, 1);
  198.   *str->ptr = 0;
  199.   return ch;
  200. }
  201.  
  202. int
  203. get_token (fp, s)
  204.      register FILE *fp;
  205.      register sstring *s;
  206. {
  207.   int c;
  208.   s->ptr = s->base;
  209.  retry:
  210.   c = ' ';
  211.   c = skip_spaces (fp, c);
  212.   if (c == '\n')
  213.     {
  214.       source_lineno++;
  215.       lineno++;
  216.       goto retry;
  217.     }
  218.   if (c == '#')
  219.     {
  220.       c = get_token (fp, s);
  221.       if (c == INT_TOKEN)
  222.     {
  223.       source_lineno = atoi (s->base) - 1; /* '\n' will add 1 */
  224.       get_token (fp, &source_filename);
  225.     }
  226.       for (;;)
  227.     {
  228.       c = getc (fp);
  229.       if (c == EOF)
  230.         return EOF;
  231.       if (c == '\n')
  232.         {
  233.         source_lineno++;
  234.         lineno++;
  235.         goto retry;
  236.         }
  237.     }
  238.     }
  239.   if (c == EOF)
  240.     return EOF;
  241.   if (isdigit (c))
  242.     {
  243.       do
  244.     {
  245.       SSTRING_PUT(s, c);
  246.       c = getc (fp);
  247.     } while (c != EOF && isdigit(c));
  248.       ungetc (c, fp);
  249.       c = INT_TOKEN;
  250.       goto done;
  251.     }
  252.   if (isalpha (c) || c == '_')
  253.     {
  254.       c = scan_ident (fp, s, c);
  255.       ungetc (c, fp);
  256.       return IDENTIFIER_TOKEN;
  257.     }
  258.   if (c == '\'' || c == '"')
  259.     {
  260.       c = scan_string (fp, s, c);
  261.       ungetc (c, fp);
  262.       return c == '\'' ? CHAR_TOKEN : STRING_TOKEN;
  263.     }
  264.   SSTRING_PUT(s, c);
  265.  done:
  266.   MAKE_SSTRING_SPACE(s, 1);
  267.   *s->ptr = 0;
  268.   return c;
  269. }
  270.  
  271. unsigned long
  272. hash (str)
  273.      char *str;
  274. {
  275.   int h = 0;
  276.   /* Replace this with something faster/better! FIXME! */
  277.   while (*str) h = (h << 3) + *str++;
  278.   return h & 0x7FFFFFFF;
  279. }
  280.